package listdemo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);

        //其他打印方式
        System.out.println("=======for循环======");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();

        System.out.println("======for each 循环=====");
        for (Integer x : list) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("=======迭代器遍历1========");
        ListIterator<Integer> lit = list.listIterator();
        while (lit.hasNext()) {
            System.out.print(lit.next() + " ");
        }
        System.out.println();

        System.out.println("======迭代器遍历2========");
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();

        System.out.println("=======迭代器遍历3========");
        ListIterator<Integer> litPrev = list.listIterator(list.size());
        while (litPrev.hasPrevious()) {
            System.out.print(litPrev.previous() + " ");
        }
        System.out.println();
    }

    public static void main3(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(4);
        list.add(3);
        list.add(2);
        list.add(1);
        System.out.println(list);

        List<Integer> listTmp = list.subList(1, 3);//截取范围：下标左闭右开
        System.out.println(listTmp);
        System.out.println("===================");
        listTmp.set(0, 99);//设置截取部分下标为0的位置为99
        System.out.println("listTmp: " + listTmp);
        System.out.println("list: " + list);

        list.remove(new Integer(1));//删除元素“1”
        System.out.println(list);

        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(11);
        list1.add(22);
        list1.add(33);
        list1.add(44);
        list.addAll(list1);//将list1中的内容全部加到list的末尾
        System.out.println(list);

        /*ArrayList<String> list2 = new ArrayList<>();
        list.addAll(list2);*/

    }

    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        list.add(4);
        list.add(3);
        list.add(2);
        list.add(1);
        System.out.println(list);

        list.add(0, 5);
        System.out.println(list);
    }

    public static void main1(String[] args) {
        MyArrayList myArrayList = new MyArrayList();

        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.display();

        System.out.println(myArrayList.contains(2));

        System.out.println(myArrayList.get(2));

        myArrayList.set(1, 99);
        myArrayList.display();
    }
}
